#!/bin/sh
#
# Check that Go files have been formatted
#

for file in `git diff-index --cached --name-only HEAD --diff-filter=ACMR| grep "\.go$"` ; do
    # nf is the temporary checkout. This makes sure we check against the
    # revision in the index (and not the checked out version).
    nf=`git checkout-index --temp ${file} | cut -f 1`
    newfile=`mktemp /tmp/${nf}.XXXXXX` || exit 1
    gofmt ${nf} > "${newfile}" 2>> /dev/null
    diff -u -p "${nf}" "${newfile}"
    r=$?
    rm "${newfile}"
    rm "${nf}"
    if [ $r != 0 ] ; then
echo "================================================================================================="
echo " Code format error in: $file                                                                      "
echo "                                                                                                 "
echo " Please fix before committing. Don't forget to run git add before trying to commit again.        "
echo " If the whole file is to be committed, this should work (run from the top-level directory):      "
echo "                                                                                                 "
echo "   go fmt $file; git add $file; git commit"
echo "                                                                                                 "
echo "================================================================================================="
        exit 1
    fi
done

for file in `git diff-index --cached --name-only HEAD --diff-filter=ACMR| grep "\.js$"` ; do
    case "$file" in
        */libs/*)
            echo "Not checking library ${file}"
            ;;
        build/*)
            echo "Not checking build ${file}"
            ;;
        *)
            echo "Checking ${file}"
            nf=`git checkout-index --temp ${file} | cut -f 1`
            PATH="node_modules/.bin:${PATH}" jshint --config .jshint "${nf}"
            r=$?
            rm "${nf}"
            if [ $r != 0 ] ; then
        echo "================================================================================================="
        echo " Code format error in: $file                                                                     "
        echo "                                                                                                 "
        echo " Please fix before committing. Don't forget to run git add before trying to commit again.        "
        echo "                                                                                                 "
        echo "================================================================================================="
                exit 1
            fi
            ;;
    esac
done


check_styles=0
for file in `git diff-index --cached --name-only HEAD --diff-filter=ACMR| grep "\.scss$"` ; do
    check_styles=1
done

if [ "${check_styles}" = "1" ]; then
    make styleshint
    r=$?
    if [ $r != 0 ] ; then
echo "================================================================================================="
echo " Error in styles                                                                                 "
echo "                                                                                                 "
echo " Please fix before committing. Don't forget to run git add before trying to commit again.        "
echo "                                                                                                 "
echo "================================================================================================="
        exit 1
    fi
fi


for file in `git diff-index --cached --name-only HEAD --diff-filter=ACMR| grep "\.po$"` ; do
    echo "Checking ${file}"
    nf=`git checkout-index --temp ${file} | cut -f 1`
    ./src/i18n/helpers/polint.py --hook "${nf}" "${file}"
    r=$?
    rm "${nf}"
    if [ $r != 0 ] ; then
echo "================================================================================================="
echo " Format error in: $file                                                                          "
echo "                                                                                                 "
echo " Please fix before committing. Don't forget to run git add before trying to commit again.        "
echo "                                                                                                 "
echo "================================================================================================="
        exit 1
    fi
done
